package com.tienon.message.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import com.tienon.message.entity.Push_App;
import com.tienon.message.thread.task.AppSendMsgTask;

/**
 * 消息发送表线程组
 * 包含app，短信，email等的加载任务
 * @author xiuye
 *
 */
public class LoadSendMsgTasks {

	private static ExecutorService pool = null;

	private static CompletionService<Boolean> csPool = null;

	static {
		pool = Executors.newFixedThreadPool(3);
		csPool = new ExecutorCompletionService<Boolean>(pool);
	}

	/**
	 * 传入单个app任务
	 * @param data
	 * @return
	 */
	public static Future<Boolean> loadAppSendMsgTask(Push_App data) {

		AppSendMsgTask task = new AppSendMsgTask(data);
		return csPool.submit(task);

	}

	/**
	 * 传入多个app任务
	 * @param datas
	 * @return
	 */
	public static List<Future<Boolean>> loadAppSendMsgTasks(List<Push_App> datas) {

		List<Future<Boolean>> futures = new ArrayList<Future<Boolean>>();

		for (Push_App data : datas) {
			
			AppSendMsgTask task = new AppSendMsgTask(data);
			futures.add(csPool.submit(task));
		}

		return futures;

	}

	/**
	 * 关闭线程组
	 */
	public static void closeTasks() {

		pool.shutdown();
		pool = null;
		csPool = null;
		
	}
	/**
	 * 载入新的线程组
	 */
	public static void openTasks(){
		pool = Executors.newFixedThreadPool(3);
		csPool = new ExecutorCompletionService<Boolean>(pool);
	}
}
